package com.dfdchem.imd.kaiwu.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.manager.GlideManager;
import com.dfdchem.imd.kaiwu.model.AppArticle;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    private final static String TAG = "NewsAdapter";
    private List<AppArticle> news;

    public NewsAdapter(List<AppArticle> news) {
        this.news = news;
    }

    public void setNews(List<AppArticle> news) {
        this.news = news;
    }
    public List<AppArticle> getNews() {
        return news;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView cover;
        public TextView title;
        public TextView summary;
        public TextView author;
        public TextView publish_time;
        public ViewHolder(View view) {
            super(view);
            cover = view.findViewById(R.id.cover);
            title = view.findViewById(R.id.title);
            summary = view.findViewById(R.id.summary);
            author = view.findViewById(R.id.author);
            publish_time = view.findViewById(R.id.publish_time);
        }
    }

    @Override
    public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_news, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AppArticle article = news.get(position);
        holder.title.setText(article.getTitle());
        holder.summary.setText(article.getSummary());
        holder.author.setText(article.getAuthor());
        holder.publish_time.setText(article.getPublishTime());
        new GlideManager(holder.itemView).load(holder.cover, article.getCover());

    }

    @Override
    public int getItemCount() {
        return news == null ? 0 : news.size();
    }

}
